package com.winhxd.b2c.common.feign.system;

import com.winhxd.b2c.common.constant.BusinessCode;
import com.winhxd.b2c.common.constant.ServiceName;
import com.winhxd.b2c.common.domain.PagedList;
import com.winhxd.b2c.common.domain.ResponseResult;
import com.winhxd.b2c.common.domain.system.user.condition.CheckUserCondition;
import com.winhxd.b2c.common.domain.system.user.condition.ResetPasswordCondition;
import com.winhxd.b2c.common.domain.system.user.condition.SysUserCondition;
import com.winhxd.b2c.common.domain.system.user.condition.SysUserResetPasswordCondition;
import com.winhxd.b2c.common.domain.system.user.dto.SysUserDTO;
import com.winhxd.b2c.common.domain.system.user.dto.SysUserPasswordDTO;
import com.winhxd.b2c.common.domain.system.user.model.SysUser;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.List;


@FeignClient(value = ServiceName.SYSTEM_SERVICE, fallbackFactory = UserServiceClientFallback.class)
public interface UserServiceClient {

    /**
     * 新增用户
     * @author zhangzhengyang
     * @date 2018/8/2
     * @param sysUserDTO
     * @return
     */
    @RequestMapping(value = "/user/2300/v1/add", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseResult<Long> saveSysUser(@RequestBody SysUserDTO sysUserDTO);

    /**
     * 修改用户
     * @author zhangzhengyang
     * @date 2018/8/2
     * @param sysUser
     * @return
     */
    @RequestMapping(value = "/user/2301/v1/update", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseResult<Void> modifySysUser(@RequestBody SysUser sysUser);

    /**
     * 修改密码
     * @author zhangzhengyang
     * @date 2018/8/2
     * @param sysUser
     * @return
     */
    @RequestMapping(value = "/user/2302/v1/updatePassword", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseResult<Void> updatePassword(@RequestBody SysUserPasswordDTO sysUser);

    /**
     * 查询用户列表
     * @author zhangzhengyang
     * @date 2018/8/2
     * @param condition
     * @return
     */
    @RequestMapping(value = "/user/2303/v1/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseResult<PagedList<SysUser>> findSysUserPagedList(@RequestBody SysUserCondition condition);

    /**
     * 根据登录账号获取用户信息
     * @author zhangzhengyang
     * @date 2018/8/2
     * @param account
     * @return
     */
    @RequestMapping(value = "/user/2304/v1/get/{account}", method = RequestMethod.GET)
    ResponseResult<SysUser> getByAccount(@PathVariable("account") String account);

    /**
     * 根据主键获取用户信息
     * @author zhangzhengyang
     * @date 2018/8/2
     * @param id
     * @return
     */
    @RequestMapping(value = "/user/2305/v1/get/{id}", method = RequestMethod.GET)
    ResponseResult<SysUser> getSysUserById(@PathVariable("id") Long id);

    /**
     * 根据主键禁用用户
     * @author zhangzhengyang
     * @date 2018/8/7
     * @param id
     * @return
     */
    @RequestMapping(value = "/user/2306/v1/disabled/{id}", method = RequestMethod.PUT)
    ResponseResult<Void> disabled(@PathVariable("id") Long id);

    /**
     * 根据主键启用用户
     * @author songkai
     * @date 2018/8/23
     * @param id
     * @return
     */
    @RequestMapping(value = "/user/2307/v1/enable/{id}", method = RequestMethod.PUT)
    ResponseResult<Void> enable(@PathVariable("id") Long id);

    /**
     * 重置密码，根据用户名发送验证码
     *
     * @param sysUserResetPasswordCondition
     * @return
     */
    @RequestMapping(value = "/user/2308/v1/sendVerifyCode", method = RequestMethod.POST)
    ResponseResult<Void> sendVerifyCode(@RequestBody SysUserResetPasswordCondition sysUserResetPasswordCondition);

    /**
     * 重置密码
     *
     * @param sysUserResetPasswordCondition
     * @return
     */
    @RequestMapping(value = "/user/2309/v1/resetPassword", method = RequestMethod.POST)
    ResponseResult<Void> resetPassword(@RequestBody SysUserResetPasswordCondition sysUserResetPasswordCondition);

    /** 根据custoemr_id查询用户sys_user信息
     * @author ghf
     * @Description
     * @date
     * @param
     * @return
     */
    @RequestMapping(value = "/user/2310/v1/getSysUserByCondition", method = RequestMethod.POST)
    ResponseResult<SysUser> getSysUserByCondition(@RequestBody CheckUserCondition condition);

    /** 根据部门查询员工接口
     * @author genghaifei
     * @Description
     * @date
     * @param
     * @return
     */
    @RequestMapping(value = "/user/2311/v1/getSysUserByOrgId", method = RequestMethod.POST)
    ResponseResult<PagedList<SysUser>> getSysUserByOrgId(@RequestBody SysUserCondition condition);



    /** 根据parendID查询某个一级分销商下面有多少二级分销商
     * @author 王泽坤
     * @Description
     * @date
     * @param
     * @return
     */
    @RequestMapping(value = "/user/2312/v1/countDistributorByParentId/{parentId}", method = RequestMethod.GET)
    ResponseResult<Integer> countDistributorByParentId(@PathVariable("parentId") Long parentId);

    /** 查询用户列表 - 不分页
     * @author genghaifei
     * @Description
     * @date
     * @param
     * @return
     */
    @RequestMapping(value = "/user/2313/v1/allList", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseResult<List<SysUser>> findSysUserList(@RequestBody SysUserCondition condition);

    @RequestMapping(value = "/user/2314/v1/modifyMobile", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseResult<Void> modifyMobileByCustomerId(@RequestBody SysUserCondition condition);

    /** 重置密码
     * @author genghaifei
     * @Description
     * @date
     * @param
     * @param currentUser
     * @return
     */
    @RequestMapping(value = "/user/2315/v1/resetUserPassword", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseResult<Void> resetUserPassword(@RequestBody ResetPasswordCondition currentUser);


    /** 还原密码 - 参数 - customerId
     * @author genghaifei
     * @Description
     * @date
     * @param
     * @return
     */
    @RequestMapping(value = "/user/2316/v1/resetUserPasswordByCustomerId", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseResult<Long> resetUserPasswordByCustomerId(@RequestBody ResetPasswordCondition condition);

    /** 根据一个值来查询账号或密码是否存在用户数据, 并且返回
     * @author genghaifei
     * @Description
     * @date
     * @param
     * @return
     */
    @RequestMapping(value = "/user/2317/v1/findUserListByCondition", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    ResponseResult<List<SysUser>> findUserListByCondition(@RequestBody CheckUserCondition condition);
}

@Component
class UserServiceClientFallback implements UserServiceClient, FallbackFactory<UserServiceClient> {
    private static final Logger logger = LoggerFactory.getLogger(UserServiceClientFallback.class);
    private Throwable throwable;

    public UserServiceClientFallback() {
    }

    private UserServiceClientFallback(Throwable throwable) {
        this.throwable = throwable;
    }

    @Override
    public ResponseResult<Long> saveSysUser(SysUserDTO sysUserDTO) {
        logger.error("UserServiceClientFallback -> save", throwable);
        return new ResponseResult<>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult modifySysUser(SysUser sysUser) {
        logger.error("UserServiceClientFallback -> modify", throwable);
        return new ResponseResult<>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult updatePassword(SysUserPasswordDTO sysUser) {
        logger.error("UserServiceClientFallback -> updatePassword", throwable);
        return new ResponseResult<>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<PagedList<SysUser>> findSysUserPagedList(SysUserCondition condition) {
        logger.error("UserServiceClientFallback -> find", throwable);
        return new ResponseResult<>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<SysUser> getByAccount(String account) {
        logger.error("UserServiceClientFallback -> getByUserCode", throwable);
        return new ResponseResult<>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<SysUser> getSysUserById(Long userId) {
        logger.error("UserServiceClientFallback -> get", throwable);
        return new ResponseResult<>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult disabled(Long id) {
        logger.error("UserServiceClientFallback -> disabled", throwable);
        return new ResponseResult(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<Void> enable(Long id) {
        logger.error("UserServiceClientFallback -> enable", throwable);
        return new ResponseResult(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<Void> sendVerifyCode(SysUserResetPasswordCondition sysUserResetPasswordCondition) {
        logger.error("UserServiceClientFallback -> sendVarifyCode", throwable);
        return new ResponseResult<>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<Void> resetPassword(SysUserResetPasswordCondition sysUserResetPasswordCondition) {
        logger.error("UserServiceClientFallback -> resetPassword", throwable);
        return new ResponseResult<>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<SysUser> getSysUserByCondition(CheckUserCondition condition) {
        logger.error("UserServiceClientFallback -> getSysUserByCondition", throwable);
        return new ResponseResult<>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<PagedList<SysUser>> getSysUserByOrgId(SysUserCondition condition) {
        logger.error("UserServiceClientFallback -> getSysUserByOrgId", throwable);
        return new ResponseResult<>(BusinessCode.CODE_1001);
    }

    @Override
    public UserServiceClient create(Throwable throwable) {
        return new UserServiceClientFallback(throwable);
    }


    @Override
    public ResponseResult<Integer> countDistributorByParentId(Long parentId) {
        logger.error("UserServiceClientFallback -> countMemberCountByParentId", throwable);
        return new ResponseResult<>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<List<SysUser>> findSysUserList(SysUserCondition condition) {
        logger.error("UserServiceClientFallback -> findSysUserList", throwable);
        return new ResponseResult<>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<Void> modifyMobileByCustomerId(SysUserCondition condition) {
        logger.error("UserServiceClientFallback -> modifyMobileByCustomerId", throwable);
        return new ResponseResult<>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<Void> resetUserPassword(ResetPasswordCondition currentUser) {
        logger.error("UserServiceClientFallback -> resetUserPassword", throwable);
        return new ResponseResult<>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<Long> resetUserPasswordByCustomerId(ResetPasswordCondition condition) {
        logger.error("UserServiceClientFallback -> resetUserPasswordByCustomerId", throwable);
        return new ResponseResult<>(BusinessCode.CODE_1001);
    }

    @Override
    public ResponseResult<List<SysUser>> findUserListByCondition(CheckUserCondition condition) {
        logger.error("UserServiceClientFallback -> findUserListByCondition", throwable);
        return new ResponseResult<>(BusinessCode.CODE_1001);
    }
}